package com.haois.mp.database.module.offaccounts.rest;

import com.google.gson.Gson;
import com.haois.mp.database.commom.enums.ApiResultEnum;
import com.haois.mp.database.commom.constant.Reply;
import com.haois.mp.database.exception.ErrorException;
import com.haois.mp.database.module.offaccounts.service.RecordService;
import me.chanjar.weixin.common.exception.WxErrorException;

import com.haois.mp.database.commom.constant.StatusCode;
import com.haois.mp.database.module.common.daomain.Subject;
import com.haois.mp.database.module.common.daomain.ShareRecord;
import com.haois.mp.database.module.remote.service.IGroupService;
import com.haois.mp.database.module.remote.service.ISysService;
import com.haois.mp.database.util.mp.ProduceJwt;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;

/**
 * Created by RuiHaoZ on 2017/12/17 12:47
 */
@RestController
@RequestMapping("/wxmp/rest")
public class InfoRestController {

    @Autowired
    private RecordService recordService;
    @Autowired
    private IGroupService iGroupService;
    @Autowired
    private ISysService iSysService;

    private ProduceJwt produceJwt = new ProduceJwt();

    Gson gson = new Gson();

    @RequestMapping(value = "/share/add", params = {"fromPage", "fromUser"})
    public String addShare(ShareRecord shareRecord) {
        recordService.addShare(shareRecord);
        return new Reply(ApiResultEnum.API_00).toJson();
    }

    @RequestMapping(value = "/invite/add", params = {"fromPage", "fromUser"})
    public String addInvite(ShareRecord shareRecord) {
        recordService.addShare(shareRecord);
        return new Reply(ApiResultEnum.API_00).toJson();
    }

    @RequestMapping("/share/getGroup")
    public Map shareGroup(String oid, HttpServletRequest request)
            throws RuntimeException, WxErrorException, ErrorException {
        String access_token = produceJwt.getShareToken();
        Map result = iGroupService.getGroup(oid, access_token);
        return result;
    }

    @RequestMapping(value = "/sys/parameter",params = {"type"})
    public Map getParameter(String type)
            throws RuntimeException, WxErrorException, ErrorException {
        String access_token = produceJwt.produceWeChatToken(new Subject(StatusCode.SHARE_NUM));
        Map result = null;
        if ("hiSlider".equals(type)) {
           result = iSysService.getParameter(access_token);
        }else {
            result = iSysService.getParameter(type, access_token);
        }
        return result;
    }

}
